package com.wskeee.ec.core
{
	import flash.display.Sprite;
	import flash.text.TextFieldAutoSize;
	
	import com.wskeee.ec.ens;
	import com.wskeee.ec.interfaces.ISkin;
	import com.wskeee.ec.interfaces.ISkinDrawer;
	import com.wskeee.ec.styles.DefaultStyle;
	import com.wskeee.ec.styles.StyleNames;
	
	public class SkinableComponent extends EComponent implements ISkin
	{
		/**
		 * 皮肤初始发 
		 */		
		protected var skinInitialize:Boolean = false;
		/**
		 * 用于处理皮肤
		 */		
		protected var skinContainer:Sprite;
		
		public function SkinableComponent()
		{
			skinContainer = addChild(new Sprite()) as Sprite;
			super();
			SkinDrawer = DefaultSkinDrawer;
		}
		
		//========================= override ====================
		protected function initSkin():void
		{
			if(!skinInitialize && SkinDrawerClass)
			{
				skinDrawer = new SkinDrawerClass() as ISkinDrawer;
				skinDrawer.init(this,skin ? skin : skinContainer);
				skinInitialize = true;
			}
		}
		
		//========================= end =========================
		
		//--------------------------------------------------------------------------
		//
		//  override Method
		//
		//--------------------------------------------------------------------------
		
		//---------------------
		//redreawSkin
		//---------------------
		/**
		 * 样式或者手动更新皮肤时调用  
		 * 
		 */
		protected function redrawSkin():void
		{
			//override
			if(skinDrawer)
				skinDrawer.redraw();
		}
		
		//========================= validate ====================
		override protected function commitProperties():void
		{
			super.commitProperties();
			if(skinDrawer)
				skinDrawer.validateProperties();
		}
		override protected function measureSize():void
		{
			super.measureSize();
			if(skinDrawer)
				skinDrawer.validateSize();
		}
		override protected function updateDisplayList():void
		{
			super.updateDisplayList();
			//第一次或者样式改变时调用 redraw() 
			if(redrawSkinFlag)
			{
				redrawSkinFlag = false;
				redrawSkin();
			}
			if(skinDrawer)
				skinDrawer.validateDisplayList();
		}
		//========================= end =========================
		
		//========================= style =======================
		private var redrawSkinFlag:Boolean = true;
		/**
		 * 手动 调用 重绘 skin 
		 */
		public function redraw():void
		{
			redrawSkinFlag = true;
		}
		override public function reflashStyle():void
		{
			super.reflashStyle();
			redraw();
		}
		override public function setStyles(value:Object):void
		{
			super.setStyles(value);
			redraw();
		}
		override public function setStyle(name:String, value:Object):void
		{
			super.setStyle(name,value);
			redraw();
		}
		//========================= end =========================
		
		//========================= implements ==================
		//---------------------
		//ISkin
		//---------------------
		protected var SkinDrawerClass:Class;
		protected var skinDrawer:ISkinDrawer;
		
		public function set SkinDrawer(value:Class):void
		{
			if(value && SkinDrawerClass!=value)
			{
				skinInitialize = false;
				SkinDrawerClass = value;
				initSkin();
				invalidateDisplayList();
			}
		}
		
		private var _skin:Object;
		public function set skin(value:Object):void
		{
			_skin = value;
			invalidateDisplayList();
		}
		
		public function get skin():Object
		{
			return _skin;
		}
		
		public function get DefaultSkinDrawer():Class
		{
			return null;
		}
		//========================= end =========================
	}
}